package com.atung.dao;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.atung.model.BusCarRide;
import com.atung.model.BusOrderCar;
import com.atung.model.BusOrderCarDTO;
import com.atung.provider.BusOrderCarProvider;
import com.atung.response.ResponeRefundDetails;
import com.atung.response.ResponeRefundDetailsIng;
import com.atung.response.ResponseBusOrderCarList;

@Mapper
public interface BusOrderCarDAO {

	/**
	 * 新增订单
	 * @param BusOrderCar
	 * @return
	 */
	@InsertProvider(type=BusOrderCarProvider.class,method="saveBusOrderCarProvider")
	Boolean saveBusOrderCar(BusOrderCar BusOrderCar);
	
	
	/**
	 * 获取订单列表
	 * @return
	 */
	@SelectProvider(type=BusOrderCarProvider.class,method="getOrderListProvider")
	List<ResponseBusOrderCarList>getOrderList(@Param("openId")String openId,@Param("orderState")Integer orderState);
	


	/**
	 * 获取订单详情
	 * @param orderId
	 * @return
	 */
	@SelectProvider(type=BusOrderCarProvider.class,method="orderDetailsProvider")
	BusOrderCarDTO getOrderDetails(@Param("orderId")Long orderId);
	
	
	
	/**
	 * 扫码上车状态变为行程中
	 * @param orderId
	 * @return
	 */
	@Update("update bus_order_car   set order_state=302 where id=#{orderId} and order_state=301 and is_delect=0")
	Integer updateBusOrderCarOrderStateIsInTravel(@Param("orderId")Long orderId);
	
	
	/**
	 * 待出行修改
	 * @param orderId
	 * @return
	 */
	@Update("update bus_order_car  set order_state=301 where id=#{orderId} and order_state=300 and is_delect=0")
	Integer updateBusOrderCarOrderStateToTrave(@Param("orderId")Long orderId);
	
	/**
	 * 根据订单编号获取订单信息
	 * @param orderId
	 * @return
	 */
	@Select("select * from bus_order_car where id=#{orderId} and is_delect=0")
	BusOrderCar selectBusOrderCarById(@Param("orderId")Long orderId);
	
	/**
	 *验证身份数据
	 * @param orderId
	 * @return
	 */
	@Select(" select bcr.* from bus_car_ride bcr inner join bus_order_car boc on bcr.order_id=boc.id "
			+ "where bcr.allocation_id=#{allocationId}  "
			+ " and bcr.ride_code =#{rideCode} and bcr.is_delect=0 and boc.order_state BETWEEN 301 and 303")
	BusCarRide selectBusCarRideByAllocationIdAndRideCode(@Param("allocationId")Long allocationId,
			@Param("rideCode")String rideCode);
	
	/**
	 * 获取同行人员
	 * @param openId
	 * @return
	 */
	@Select("select * from bus_car_ride where order_id=#{orderId} and  is_delect=0 ")
	List<BusCarRide> selectBusCarRideByOrderId(@Param("orderId")Long orderId);
	
	/**
	 * 修改上车状态
	 * @param id
	 * @return
	 */
	@Update("update bus_car_ride set is_ride=1 ,modification_time=#{modificationTime}  where id =#{id} and is_ride=0 and is_delect=0")
	Boolean updateBusCarRideByisRide(@Param("id")Long id,@Param("modificationTime")LocalDateTime modificationTime);


	/**
	 * 获取退款详情
	 * @param orderId
	 * @return
	 */
	@Select("select brp.departl_name,brp.destination,brp.start_data,bsbp.order_pay_money refundPrincipal,boc.order_state  from bus_order_car boc  inner join bus_school_bus_payment bsbp on boc.payment_id=bsbp.id  inner join bus_rout_publish  brp on brp.id=bsbp.rout_publish_id  "
			+ "where boc.id=#{orderId} and (boc.order_state=300 or boc.order_state=301) and bsbp.order_status=202")
	ResponeRefundDetails refundDetails(@Param("orderId")Long orderId);
	
	
	/**
	 * 退款中详情
	 * @param orderId
	 * @return
	 */
	@Select("select  bsbp.id platformOrderNumber,brp.departl_name,brp.destination, brp.start_data,bor.refund_fee ,bor.refund_state,bor.refund_desc,bor.refund_time  "
			+ "from   bus_order_refund  bor  inner join bus_order_car boc on bor.order_id=boc.id "
			+ "inner join bus_school_bus_payment bsbp on boc.payment_id=bsbp.id  "
			+ "inner join bus_rout_publish brp on brp.id=bsbp.rout_publish_id "
			+ "  where bor.order_id=#{orderId}  and boc.order_state>=304 and bsbp.order_status>=202")
	ResponeRefundDetailsIng refundDetailsIng(@Param("orderId")Long orderId);
}
